﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NLog;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Collections;

namespace Pilot_Chat
{
    class MyEventArgs : EventArgs
    {
        private TcpClient _sock;
        public MyEventArgs(TcpClient sock)
        {
            _sock = sock;
        }
        public TcpClient sock
        {
            get { return _sock; }
            set { _sock = value; }
        }
    }

    class ChatServer
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        const int LISTEN_PORT = 3513;
        private TcpListener _listener;
        private TcpClient _client_sock; // tmp
        private Thread _listen_thread;
        private ArrayList _users = new ArrayList();
        private ArrayList _threads = new ArrayList();

        public ChatServer()
        {
        }

        public void ThreadProc(Object obj)
        {
            log.Trace("ThreadProc");
            OnClientAdded(this, new MyEventArgs((TcpClient)obj));
        }

        public void OnClientAdded(object sender, EventArgs e)
        {
            TcpClient sock = ((MyEventArgs)e).sock;
            IPEndPoint endpoint = (IPEndPoint)sock.Client.RemoteEndPoint;

            log.Trace("OnClientAdded: from {0}:{1}", endpoint.Address.ToString(), endpoint.Port);

            _users.Add(sock);
            _threads.Add(Thread.CurrentThread);

            //sock.Client.BeginReceive(...);
        }

        public void Listen()
        {
            _listener = new TcpListener(IPAddress.Parse("127.0.0.1"), LISTEN_PORT);
            _listener.Start();
            log.Warn("Listen: {0}", LISTEN_PORT);

            while (true)
            {
                Thread th = new Thread(new ParameterizedThreadStart(ThreadProc));
                TcpClient client = _listener.AcceptTcpClient();
                log.Warn("AcceptTcpClient");
                th.Start(client);
            }
        }
        public void OnReceive(IAsyncResult ar)
        {
        }

        public void Start()
        {
            _listen_thread = new Thread(new ThreadStart(Listen));
            _listen_thread.Start();
        }
    }

    class Program
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            log.Info("Begin....");

            ChatServer svr = new ChatServer();
            svr.Start();
/*
            log.Trace("Sample trace message");
            log.Debug("Sample debug message");
            log.Info("Sample informational message");
            log.Warn("Sample warning message");
            log.Error("Sample error message");
            log.Fatal("Sample fatal error message");
*/
            log.Info("-------------------------------------------");
            log.Info("Press ENTER to exit...");
            Console.ReadKey();
        }
    }
}
